package org.zjx.util;

import lombok.SneakyThrows;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class PropImageUtil {
    public static final int CELL_SIZE = 50;

    /**
     * 获取道具栏中所有格子的图片
     *
     * @return 按从左到右、从上到下顺序排列的格子图片列表
     */
    public static List<BufferedImage> getLatticeImages(BufferedImage bufferedImage) {
        int width = latticeNumber(bufferedImage.getWidth());
        int height = latticeNumber(bufferedImage.getHeight());

        List<BufferedImage> bufferedImages = new ArrayList<>(width * height);

        // 每个格子实际占用 cellSize + 1 像素（50像素格子 + 1像素间隔）
        int totalCellSize = CELL_SIZE + 1;

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                // 计算每个格子的坐标和大小
                int x = col * totalCellSize;
                int y = row * totalCellSize;

                // 处理边缘格子不需要额外1像素间隔的情况
                boolean isLastCol = (col == width - 1);
                boolean isLastRow = (row == height - 1);

                int cutWidth = isLastCol ? CELL_SIZE : totalCellSize;
                int cutHeight = isLastRow ? CELL_SIZE : totalCellSize;

                // 截取格子图片
                BufferedImage image = ImageUtil.cutImage(
                        bufferedImage,
                        new Rectangle(x, y, cutWidth + 1, cutHeight + 1)
                );

                bufferedImages.add(image);
            }
        }

        return bufferedImages;
    }

    /**
     * 获取指定索引的格子区域
     *
     * @param bufferedImage 道具栏图片
     * @param index         格子索引（从左到右，从上到下顺序）
     * @return 对应格子的 Rect区域对象
     * @throws ArrayIndexOutOfBoundsException 当索引超出范围时抛出
     */
    public static Rectangle getIndexRect(BufferedImage bufferedImage,  int index) {
        // 计算总格子数
        int width = latticeNumber(bufferedImage.getWidth());
        int height = latticeNumber(bufferedImage.getHeight());
        int totalNumber = width * height;

        // 索引有效性检查
        if (index < 0 || index >= totalNumber) {
            throw new ArrayIndexOutOfBoundsException("格子索引超出范围: " + index + " (总数: " + totalNumber + ")");
        }

        // 计算格子的行列数
        int widthCount = latticeNumber(bufferedImage.getWidth());

        // 计算行列位置
        int row = index / widthCount;
        int col = index % widthCount;

        // 计算格子实际位置（考虑1像素间隔）
        int totalCellSize = CELL_SIZE + 1;
        int x = col * totalCellSize;
        int y = row * totalCellSize;

        // 返回格子区域（最后一个格子可能不需要+1像素间隔）
        boolean isLastCol = (col == widthCount - 1);
        boolean isLastRow = (row == latticeNumber(bufferedImage.getHeight()) - 1);

        int cutWidth = isLastCol ? CELL_SIZE : totalCellSize;
        int rectHeight = isLastRow ? CELL_SIZE : totalCellSize;

        return new Rectangle(x, y, cutWidth + 1, rectHeight + 1);
    }

    /**
     * 计算单维度（宽度或高度）上的格子数量
     *
     * @param len 图片宽度或高度（像素）
     * @return 该维度上的格子数量
     */
    @SneakyThrows
    private static int latticeNumber(int len) {
        if (len < CELL_SIZE + 2)
            throw new IllegalArgumentException("提供的道具栏区域格子小于单个格子" + CELL_SIZE + "像素！！！");
        return (len - 1) / (CELL_SIZE + 1);
    }
}
